//知识点
module.exports = function (app, models, $, db, nm) {
    //数组转对象
    function array2Object(arr,key){
        let rc = {}
        for(let i = 0; i < arr.length; i ++){
            rc[arr[i][key]] = arr[i]
        }
        return rc
    }
    //新闻详情
    app.get('/v1/students/chapter',async function(req,res){
        let studentId = req.query.studentId,courseId = req.query.courseId
        if(studentId && courseId){
            //学生得分
            let studentGrade = array2Object(await db._evalSync(`
                select students_topic_grade.grade,sp_topic.sp_chapter_id from
                (SELECT sum(grade) as grade,sp_topic_id 
                FROM sp_practice_grade 
                where sp_students_id = '${ studentId }'
                group by sp_topic_id) as students_topic_grade
                left join sp_topic 
                on sp_topic.id = students_topic_grade.sp_topic_id
                where sp_topic.sp_course_id = '${ courseId }'`),
                'sp_chapter_id')
            //知识点出题分
            let chapterGrade = array2Object(await db._evalSync(`
                SELECT sp_topic.sp_chapter_id,sum(practice_topic.grade) as grade
                FROM practice_topic
                left join sp_topic on sp_topic.id = practice_topic.sp_topic_id
                where sp_topic.sp_course_id = '${ courseId }'
                group by sp_topic.sp_chapter_id`),
                'sp_chapter_id')
            //构建知识节点树
            let chapter = await models.sp_chapter.listSync({ sp_course_id: courseId },{ sort: { id: -1 } })
            let chapterObj = {}//知识点对象
            for(let i = 0; i < chapter.length; i ++){
                chapter[i].index = i
                chapter[i].children = []
                chapterObj[chapter[i].id] = chapter[i]
                if(chapterObj[chapter[i].parent_id]) chapterObj[chapter[i].parent_id].children.push(chapter[i])
                chapter[i].totalGrade = chapterGrade[chapter[i].id] 
                    && chapterGrade[chapter[i].id].grade ? chapterGrade[chapter[i].id].grade : 0
                chapter[i].grade = studentGrade[chapter[i].id] 
                    && studentGrade[chapter[i].id].grade ? studentGrade[chapter[i].id].grade : 0
            }
            //分值更新函数
            function updateParentNode(node){
                if(node.parent_id && chapterObj[node.parent_id]){
                    let parent = chapterObj[node.parent_id]
                    parent.totalGrade += node.totalGrade
                    parent.grade += node.grade
                    updateParentNode(parent)
                }
            }
            //从叶子节点开始反向更新得分
            for(let i = 0; i < chapter.length; i ++) if(!chapter[i].children.length) updateParentNode(chapter[i])
            //计算百分比
            for(let i = 0; i < chapter.length; i ++){
                chapter[i].score = !chapter[i].totalGrade ? 0 : parseInt(chapter[i].grade / chapter[i].totalGrade * 100)
            }
            res.send(chapter)
        }else{
            res.send(`参数错误`)
        }
    })
}